<!--
 * @Descripttion: 收款管理
 * @version: @ 1.0.0
 * @Author: ZJZ
 * @Date: 2021-06-18 09:17:01
 * @LastEditors: 朱江洲
 * @LastEditTime: 2022-06-01 14:49:01
-->
<template>
  <div class="app-container">
    <div class="header">
      <div class="bread" @click="sk">
        <i
          :class="[paymentShow ? 'active' : '']"
          class="iconfont icon-xingzhuangjiehe"
        ></i>
        <span style="margin-left: 10px">收款管理</span>
      </div>
      <div class="bread" style="margin-left: 20px" @click="fp">
        <i
          class="iconfont icon-bianzu22"
          :class="[paymentShow ? '' : 'active1']"
        ></i>
        <span style="margin-left: 10px">待分配</span>
      </div>
      <div class="ipt">
        <el-input
          v-model="input"
          placeholder="请输入手机号/订单号"
          size="normal"
          clearable
          class="ipt-el"
        ></el-input>
        <el-button type="primary" size="default" @click="search"
          >搜索</el-button
        >
      </div>
      <div class="btn">
        <el-dropdown>
          <el-button class="el-icon-more"></el-button>
          <el-dropdown-menu slot="dropdown">
            <el-dropdown-item
              v-hasPermi="['order:collection:export']"
              @click.native="getExl"
            >
              导出
            </el-dropdown-item>
          </el-dropdown-menu>
        </el-dropdown>
      </div>
    </div>
    <div v-if="chs == 1" class="sel">
      <span> 已选中 {{ multipleSelection.length }} 项</span>
      <el-button
        type="primary"
        size="default"
        @click="enterD"
        plain
        :disabled="disabled"
        v-hasPermi="['order:collection:confirm']"
      >
        确认订单
      </el-button>
      <el-button
        type="primary"
        size="default"
        @click="Change"
        plain
        :disabled="disabled"
        v-hasPermi="['order:collection:replace']"
      >
        更换负责人
      </el-button>
      <el-button
        type="primary"
        plain
        size="default"
        @click="addTeam"
      >
        添加团队成员
      </el-button>
    </div>
    <div v-else-if="chs == 2" class="sel">
      <span> 已选中 {{ multipleSelection.length }} 项</span>
      <el-button
        type="primary"
        size="default"
        @click="distributionOrder"
        :disabled="disabled"
        plain
        v-hasPermi="['order:collection:distribution']"
      >
        分配订单
      </el-button>
      <el-button
        type="primary"
        size="default"
        @click="receiveOrder"
        :disabled="disabled"
        plain
        v-hasPermi="['order:collection:receive']"
      >
        领取订单
      </el-button>
    </div>
    <div class="chooise" v-else>
      <span>场景：</span>
      <el-select
        v-model="value"
        ref="sel"
        clearable
        @visible-change="(v) => visibleChange(v, 'sel', selClick, admClick)"
        class="sel-sence"
        @change="querySceneList"
      >
        <el-option
          v-for="item in options"
          :key="item.sceneId"
          :label="item.name"
          :value="item.sceneId"
        >
        </el-option>
      </el-select>
      <el-button
        type="primary"
        icon="iconfont icon-shaixuan"
        size="default"
        @click="screen"
      >
        高级筛选
      </el-button>
    </div>
    <div v-if="scenArr !== []" class="scenF">
      <div v-for="(item, index) in scenArr" :key="index" class="scenF-content">
        <div class="scenF-detail" @click="scenSearch(item.sceneId)">
          {{ item.sceneList }}
          <i
            class="el-icon-circle-close"
            @click.stop="scenDel(index)"
            style="margin-left: 10px"
          ></i>
        </div>
      </div>
    </div>
    <div class="table">
      <el-table
        :data="collData"
        @selection-change="handleSelectionChange"
        :height="tableHight"
      >
        <template slot="empty" class="emptyBg">
          <img src="@/assets/images/empty.jpg" alt="" />
        </template>
        <el-table-column type="selection"></el-table-column>
        <template v-for="(item, index) in switchShow">
          <el-table-column
            :key="index"
            :label="item.label"
            v-if="showH(item, index)"
            :prop="item.stage"
            :width="item.width"
            :show-overflow-tooltip="true"
            fixed
          >
            <template slot-scope="scope">
              <span class="batch" @click="drawer(scope.row)">
                {{ scope.row.payer }}
              </span>
            </template>
          </el-table-column>
          <el-table-column
            :key="index"
            :label="item.label"
            :width="item.width"
            :prop="item.stage"
            v-show="item.value"
            v-if="item.label !== '付款人' && item.value"
            :show-overflow-tooltip="true"
          >
          </el-table-column>
        </template>
        <el-table-column fixed="right">
          <template slot="header">
            <i class="iconfont icon-shujupeizhi" @click.stop="tableShow"></i>
          </template>
        </el-table-column>
      </el-table>
      <div class="wuhu">
        <table-sh
          :dialog="dialog"
          :switchShow.sync="switchShow"
          v-if="dialog.dialogTableVisible"
          @submit="getSwitchList"
        ></table-sh>
      </div>
    </div>
    <div class="footer">
      <el-pagination
        prev-text="上一页"
        next-text="下一页"
        background
        :page-size="pageSize"
        :page-sizes="[10, 20, 30, 40, 50, 100]"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="pageNum"
        layout=" ->,total,sizes, prev, pager, next, jumper "
        :total="total"
      >
      </el-pagination>
    </div>
    <collection-drawer
      :dialog="dialog"
      :paymentShow="paymentShow"
      :drawerData="drawerData"
      v-if="dialog.colletionShowEject"
    ></collection-drawer>
    <screen
      :dialog="dialog"
      :switchShow="switchShow"
      @sceneArr="sceneArr"
    ></screen>
    <!-- 更换负责人 -->
    <el-dialog
      title="更换负责人"
      :visible.sync="dialog.dialogPerCha"
      width="50%"
      @close="perClose"
      custom-class="dialogClass"
      :close-on-click-modal="false"
    >
      <charge-person
        v-if="dialog.dialogPerChaEject"
        ref="chaPer"
      ></charge-person>
      <span slot="footer">
        <el-button @click="dialog.dialogPerCha = false"> 取消 </el-button>
        <el-button type="primary" @click="onChangeChar"> 确定 </el-button>
      </span>
    </el-dialog>
    <el-dialog
      title="确认订单"
      :visible.sync="dialog.dialogEnter"
      width="30%"
      @close="perClone"
      style="margin-left: 300px; margin-top: 200px"
      custom-class="dialogClass"
      :close-on-click-modal="false"
    >
      <div>
        <div style="margin-bottom: 10px">银行流水号</div>
        <el-input
          v-model="serialNumber"
          placeholder="请输入银行流水号"
        ></el-input>
      </div>
      <span slot="footer">
        <el-button @click="dialog.dialogEnter = false">取消</el-button>
        <el-button type="primary" @click="enterOrder">确定</el-button>
      </span>
    </el-dialog>
    <drag-table
      :dialog="dialog"
      v-if="dialog.senceAdminEject"
      ref="dragTable"
    ></drag-table>
    <!-- <el-dialog
      title="分配订单"
      :visible.sync="dialog.distributionShow"
      width="35%"
      @close="perClone"
      style="margin-left: 300px; margin-top: 200px"
      custom-class="dialogClass"
      :close-on-click-modal="false"
    >
      <el-select
        v-model="charge"
        placeholder="请选择负责人"
        clearable
        filterable
        class="chaSel"
        style="width: 330px"
      >
        <el-option
          v-for="item in chargeList"
          :key="item.userId"
          :label="item.nickName"
          :value="item.userId"
        >
        </el-option>
      </el-select>
      <span slot="footer">
        <el-button @click="dialog.distributionShow = false">取消</el-button>
        <el-button type="primary" @click="distributionSubmit">确定</el-button>
      </span>
    </el-dialog> -->
    <allocation
      :dialog="dialog"
      :chargeId="chargeId"
      v-if="dialog.distributionShowEject"
    ></allocation>
    <join-team
      :dialog="dialog"
      v-if="dialog.joinTeamShowEject"
      :drawerData="drawerData"
      :type="type"
      :entities='entities'
    ></join-team>
    <transfer
      :dialog="dialog"
      :drawerData="drawerData"
      :type="type"
      v-if="dialog.transferShowEject"
    ></transfer>
  </div>
</template>

<script>
import {
  getCollectList,
  queryScene,
  distriBution,
  bindingPersonLiable,
  getDetail,
  editOrder,
  distribution,
  receive,
  skExport,
} from "@/api/order";
import TableSh from "./components/tableSh.vue";
import collectionDrawer from "./components/collectionDrawer.vue";
import Screen from "./components/screen.vue";
import DragTable from "./components/collection/dragTable.vue";
import Allocation from "./components/collection/allocation.vue";
// 负责人
import chargePerson from "@/components/chargePerson";
import joinTeam from "@/components/joinTeam/index.vue";
import Transfer from "@/components/joinTeam/transfer.vue";
export default {
  components: {
    collectionDrawer,
    TableSh,
    Screen,
    DragTable,
    Allocation,
    chargePerson,
    joinTeam,
    Transfer,
  },
  data() {
    return {
      type: 4,
      tableHight: window.innerHeight - 330,
      defaultParams: {
        label: "name",
        value: "sceneId",
        children: "sceneDataList",
        expandTrigger: "hover",
        checkStrictly: true,
      },
      dialog: {
        colletionShow: false,
        dialogTableVisible: false,
        dialogScreenVisible: false,
        dialogPerCha: false,
        dialogPerChaEject: false,
        dialogEnter: false,
        senceAdmin: false, //场景管理
        senceAdminEject: false,
        colletionShowEject: false,
        distributionShow: false,
        distributionShowEject: false,
        joinTeamShowEject: false,
        joinTeamShow: false,
        editTeamShowEject: false,
        editTeamShow: false,
        transferShow: false,
        transferShowEject: false,
      },
      scenArr: [],
      chargeId: "",
      serialNumber: "",
      drawerData: {},
      disabled: false,
      input: "",
      value: "",
      charge: "", //负责人
      chargeList: [],
      arr: [],
      chs: 3,
      status: 2,
      paymentShow: true,
      multipleSelection: [],
      options: [],
      // 控制表单的显示与隐藏
      switchShow: [
        {
          value: true,
          label: "付款人",
          stage: "payer",
        },
        {
          value: true,
          label: "订单号",
          stage: "orderNumber",
        },
        {
          value: true,
          label: "支付类型",
          stage: "paymentType",
        },
        {
          value: true,
          label: "手机号",
          stage: "phone",
        },
        {
          value: true,
          label: "单位",
          stage: "organizationName",
        },
        {
          value: true,
          label: "订单来源",
          stage: "orderSource",
        },
        {
          value: true,
          label: "支付时间",
          stage: "paymentTime",
        },
        {
          value: true,
          label: "总金额",
          stage: "total",
        },
        {
          value: true,
          label: "账单是否确认",
          stage: "orderStatus",
        },
        {
          value: true,
          label: "银行流水号",
          stage: "serialNumber",
        },
        {
          value: true,
          label: "负责人",
          stage: "userName",
        },
        {
          value: true,
          label: "更新时间",
          stage: "updateTime",
        },
      ],
      pageNum: 1,
      pageSize: 10,
      total: 0,
      collData: [],
    };
  },
  watch: {
    input(newValue, oldValue) {
      if (newValue == "") {
        this.getList();
      }
    },
  },
  created() {
    this.getList();
    this.getScene();
    this.onCharge();
  },
  mounted() {
    document.addEventListener("click", this.bodyCloseMenus);
    window.onresize = () => {
      return (() => {
        this.tableHight = window.innerHeight - 330;
      })();
    };
  },
  beforeDestroy() {
    document.removeEventListener("click", this.bodyCloseMenus);
  },
  activated() {
    this.tableHight = window.innerHeight - 330;
  },
  methods: {
    // 新增团队
    addTeam() {
      this.dialog.joinTeamShowEject = true;
      this.$nextTick(() => {
        this.dialog.joinTeamShow = true;
      });
    },
    // 关闭更换负责人
    perClose() {
      this.charge = "";
      this.dialog.dialogPerChaEject = false;
    },
    // 点击空白
    bodyCloseMenus(e) {
      let self = this;
      if (self.dialog.dialogTableVisible == true) {
        self.dialog.dialogTableVisible = false;
      }
    },
    // 导出所有
    getExl() {
      let that = this;
      this.$confirm("是否确认导出所有角色数据项?", "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(function () {
          return skExport({
            status: that.status,
          });
        })
        .then((response) => {
          // console.log(response);
          this.download(response.msg);
        });
    },
    search() {
      this.getList();
    },
    // 领取订单
    async receiveOrder() {
      const confirmResult = await this.$confirm(
        "你确认领取该订单吗？",
        "提示",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      ).catch((err) => err);
      /*如果用户确认打印confirm,如果用户取消显示cancel*/
      if (confirmResult !== "confirm") {
        return this.$message.info("已取消领取!");
      } else {
        receive({
          id: this.chargeId,
        }).then((res) => {
          this.$message.success(res.msg);
          this.getList();
        });
      }
      // console.log('确认了删除')
    },
    // 分配订单确认
    distributionSubmit() {
      if (this.charge == "") {
        return this.$message.error("请选择负责人");
      } else {
        distribution({
          id: this.chargeId,
          userId: this.charge,
        }).then((res) => {
          this.$message.success(res.msg);
          this.getList();
          this.dialog.distributionShow = false;
        });
      }
    },
    // 分配订单
    distributionOrder() {
      this.dialog.distributionShowEject = true;
      this.$nextTick(function () {
        this.dialog.distributionShow = true;
      });
    },
    // 点击临时搜索
    scenSearch(id) {
      this.getList(id);
    },
    // 删除临时搜索
    scenDel(i) {
      this.scenArr.splice(i, 1);
      if (this.scenArr.length == 0) {
        this.getList();
      }
    },
    // 获取数组
    sceneArr(data) {
      this.scenArr.push(data);
    },
    querySceneList() {
      this.getList(this.value);
    },
    //管理
    admClick() {
      this.dialog.senceAdminEject = true;
      this.$nextTick(function () {
        this.dialog.senceAdmin = true;
      });
    },
    //新建场景
    selClick() {
      this.screen();
    },
    // 场景下拉框按钮
    visibleChange(visible, refName, onClick, onClick2) {
      if (visible) {
        const ref = this.$refs[refName];
        let popper = ref.$refs.popper;
        if (popper.$el) popper = popper.$el;
        if (
          !Array.from(popper.children).some(
            (v) => v.className === "el-cascader-menu__list"
          )
        ) {
          const el = document.createElement("ul");
          el.className = "el-cascader-menu__list";
          el.style =
            "border-top: solid 1px #E4E7ED; padding:0; color: #606266;";
          el.innerHTML = `<li class="el-cascader-node" >
                            <span class="el-cascader-node__label" style='color:#1267FA'>新建场景</span>
                            </li>
                        `;
          const el2 = document.createElement("ul");
          el2.className = "el-cascader-menu__list";
          el2.innerHTML = `<li class="el-cascader-node" >
                            <span class="el-cascader-node__label" style='color:#1267FA'>管理</span>
                            </li>
                        `;
          popper.appendChild(el);
          popper.appendChild(el2);
          el2.onclick = () => {
            onClick2 && onClick2();
            if (ref.toggleDropDownVisible) {
              ref.toggleDropDownVisible(false);
            } else {
              ref.visible = false;
            }
          };
          el.onclick = () => {
            // 底部按钮的点击事件 点击后想触发的逻辑也可以直接写在这
            onClick && onClick();
            // 以下代码实现点击后弹层隐藏 不需要可以删掉
            if (ref.toggleDropDownVisible) {
              ref.toggleDropDownVisible(false);
            } else {
              ref.visible = false;
            }
          };
        }
      }
    },
    enterOrder() {
      if (this.serialNumber == "")
        return this.$message.error("请输入银行流水号");
      editOrder({
        serialNumber: this.serialNumber,
        id: this.chargeId,
      }).then((res) => {
        this.$message.success(res.msg);
        this.getList();
        this.dialog.dialogEnter = false;
      });
    },
    enterD() {
      this.dialog.dialogEnter = true;
    },
    perClone() {
      this.charge = "";
    },
    Change() {
      this.dialog.transferShowEject = true;
      this.$nextTick(function () {
        this.dialog.transferShow = true;
      });
    },
    handleChange(val) {
      this.value = val[0];
      this.getList(val[0], this.input);
    },
    // 请求场景
    getScene() {
      queryScene({
        type: 4,
      }).then((res) => {
        if (res.default !== null) {
          res.scenes.unshift(res.default);
          this.options = res.scenes;
        } else {
          this.options = res.scenes;
        }
      });
    },
    screen() {
      this.dialog.dialogScreenVisible = true;
    },
    getList(val) {
      getCollectList({
        pageNum: this.pageNum,
        pageSize: this.pageSize,
        sceneId: val,
        identification: this.input,
        status: this.status,
      }).then((res) => {
        if (res.rows !== []) {
          res.rows.map((item) => {
            if (item.paymentType == 1) {
              item.paymentType = "微信";
            } else if (item.paymentType == 2) {
              item.paymentType = "支付宝";
            } else {
              item.paymentType = "线下支付";
            }
            if (item.orderStatus == 1) {
              item.orderStatus = "未确认";
            } else {
              item.orderStatus = "已确认";
            }
          });
        }
        this.collData = res.rows;
        this.total = res.total;
      });
    },
    sk() {
      this.paymentShow = true;
      this.multipleSelection = [];
      this.status = 2;
      this.getList();
    },
    fp() {
      this.paymentShow = false;
      this.multipleSelection = [];
      this.status = 1;
      this.getList();
    },
    handleSizeChange(val) {
      this.pageSize = val;
      this.pageNum = 1;
      this.getList();
    },
    //currentPage 改变时会触发
    handleCurrentChange(val) {
      this.pageNum = val;
      this.getList();
    },
    tableShow() {
      if (this.dialog.dialogTableVisible == false) {
        this.dialog.dialogTableVisible = true;
      } else {
        this.dialog.dialogTableVisible = false;
      }
    },
    getSwitchList(list) {
      this.$set(this, "switchShow", list);
    },
    showH(item, index) {
      let bole = false;
      if (item.label === "付款人") {
        bole = true;
      }
      if (!item.value) bole = false;
      return bole;
    },
    updataSubmit(data) {
      this.switchShow = data;
    },
    toggleSelection(rows) {
      if (rows) {
        rows.forEach((row) => {
          this.$refs.multipleTable.toggleRowSelection(row);
        });
      } else {
        this.$refs.multipleTable.clearSelection();
      }
    },
    handleSelectionChange(val) {
      this.multipleSelection = val;
      this.entities=[]
      if (val.length !== 0) {
        this.chargeId = val[0].id;
        this.drawerData = val[0];
        val.forEach(item=>{
          this.entities.push(item.id)
        })
      }
      if (val.length !== 0 && this.paymentShow == true) {
        this.chs = 1;
      } else if (val.length !== 0 && this.paymentShow == false) {
        this.chs = 2;
      } else {
        this.chs = 3;
      }
      if (val.length == 0) {
        this.selShow = true;
      }
      if (val.length > 1) {
        this.disabled = true;
      } else {
        this.disabled = false;
      }
    },
    drawer(row) {
      this.dialog.colletionShowEject = true;
      this.drawerData = row;
      // getDetail(row.id).then((res) => {});
      this.$nextTick(function () {
        this.dialog.colletionShow = true;
      });
    },
    onCharge() {
      distriBution().then((res) => {
        this.chargeList = res.data;
      });
    },
    // 更换负责人
    onChangeChar() {
      if (this.charge == "") return this.$message.error("请选择负责人");
      bindingPersonLiable({
        id: this.chargeId,
        userId: this.charge,
      }).then((res) => {
        if (res.code != 200) return this.$message.error("更换失败");
        this.$message.success(res.msg);
        this.getList();
        this.dialog.dialogPerCha = false;
      });
    },
  },
};
</script>

<style lang="less" scoped>
.header {
  width: 100%;
  height: 74px;
  display: flex;
  justify-content: space-between;
  border-bottom: 1px solid #ececec;
  .bread {
    margin-top: 20px;
    margin-left: 5px;
    height: 30px;
    display: flex;
    vertical-align: middle;
    line-height: 30px;
    i {
      font-size: 30px;
      color: #bdbdbd;
    }
    p {
      margin-left: 10px;
      font-size: 16px;
      font-family: PingFangSC-Medium, PingFang SC;
      font-weight: 500;
      color: #2b2b2b;
    }
  }

  .ipt {
    margin: auto;
    margin-top: 16px;
    width: 340px;
    transform: translateX(-20%);
    /deep/ .el-input__inner {
      border-radius: 4px 0 0 4px;
    }
    .el-button--primary {
      border-radius: 0 4px 4px 0;
    }
  }
}
.chooise {
  margin-top: 20px;
  .sel {
    margin: 0 14px;
  }
  .el-button {
    font-size: 14px;
    /deep/ span {
      margin-left: 5px;
    }
  }
}
.active {
  color: #1172fb !important;
}
.active1 {
  color: #ff8e43 !important;
}
.sel {
  margin-top: 20px;
  span {
    margin-right: 40px;
  }
}
.footer {
  height: 30px;
  .el-pagination {
    margin-top: 24px;
    /deep/ .el-pagination__total {
      width: 40px;
      height: 40px;
      line-height: 40px;
      text-align: center;
      margin-right: 25px;
    }
    /deep/ .btn-prev {
      width: 80px;
      height: 40px;
      text-align: center;
    }
    /deep/ .btn-next {
      width: 80px;
      height: 40px;
      text-align: center;
    }
    /deep/ .number {
      width: 40px;
      height: 40px;
      line-height: 40px;
      text-align: center;
    }
    /deep/ .el-input__inner {
      // width: 40px;
      height: 40px;
      line-height: 40px;
      text-align: center;
    }
    /deep/ .more {
      width: 40px;
      height: 40px;
      line-height: 40px;
      text-align: center;
    }
    /deep/ .el-pagination__jump {
      .el-input__inner {
        width: 40px;
        height: 40px;
      }
    }
  }
}
.batch {
  color: #1267fa;
}
.btn {
  margin-top: 15px;
}
.table {
  position: relative;
  .wuhu {
    position: absolute;
    top: 45px;
    right: 0px;
    z-index: 9999;
    background-color: #fff;
    border: 1px solid #ececec;
  }
}
.ipt-el {
  width: 240px;
}
.sel-sence {
  margin-right: 10px;
}
.scenF {
  margin-top: 10px;
  display: flex;
  flex-wrap: wrap;
  .scenF-content {
    display: flex;
    margin-left: 10px;
    margin-top: 10px;
    .scenF-detail {
      height: 36px;
      line-height: 36px;
      padding-left: 20px;
      padding-right: 20px;
      border: 1px solid #9aa1af;
      font-size: 14px;
      font-family: PingFangSC-Regular, PingFang SC;
      font-weight: 400;
      color: #666666;
      .el-icon-circle-close {
        transform: translateY(3px);
      }
    }
  }
}
</style>
